{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [xls](08.01-xls.ipynb) | [Contents](Index.ipynb) | [NetCDF](08.03-NetCDF.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.2 Text文件\n",
    "\n",
    "有一些软件/工具从text文件中读取输入，然后写入text文件。如果我们想要使用这些工具同时处理(处理许多文件)，那么我们需要修改输入的text文件，并从工具编写的文件中提取信息。\n",
    "\n",
    "在跳入许多事情之前，首先我们将只需读取text文件。`r`用于读取，`w`用于写入，`a`用于附加到现有的文件中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "NUM_RUNS = 1\n",
      "\n",
      "\n",
      "\n",
      "BEGIN\n",
      "\n",
      "INPUT_FILENAME = /home/tomer/data/input.hdf\n",
      "\n",
      "OBJECT_NAME = MOD_Grid_BRDF|\n",
      "\n",
      "FIELD_NAME = Albedo_BSA_Band1\n",
      "\n",
      "BAND_NUMBER = 1\n",
      "\n",
      "SPATIAL_SUBSET_UL_CORNER = ( 13.0 75.0 )\n",
      "\n",
      "SPATIAL_SUBSET_LR_CORNER = ( 11.0 78.0 )\n",
      "\n",
      "RESAMPLING_TYPE = BI\n",
      "\n",
      "OUTPUT_PROJECTION_TYPE = UTM\n",
      "\n",
      "ELLIPSOID_CODE = WGS84\n",
      "\n",
      "UTM_ZONE = 0\n",
      "\n",
      "OUTPUT_PROJECTION_PARAMETERS = ( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0  )\n",
      "\n",
      "OUTPUT_FILENAME = /home/tomer/data/output.tif\n",
      "\n",
      "OUTPUT_TYPE = GEO\n",
      "\n",
      "END\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "fname_read = 'datas/Albedo.prm'\n",
    "f_read = open(fname_read,'r')\n",
    "for line in f_read:\n",
    "    print(line)\n",
    "f_read.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们看到该文件定义了许多参数，这些参数将被某些工具用于处理输入的hdf影像。让我们说，我们有四个输入文件，我们想批量处理它们。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fname_input = ['input0','input1','input2','input3']\n",
    "fname_output = ['output0','output1','output2','output3']\n",
    "\n",
    "for i in range(len(fname_input)):\n",
    "    \n",
    "    fname_read = 'datas/Albedo.prm'\n",
    "    f_read = open(fname_read,'r')\n",
    "    \n",
    "    fname_write = 'datas/Albedo_ii.prm'.replace('ii',str(i))\n",
    "    f_write = open(fname_write,'w')\n",
    "    for line in f_read:\n",
    "        if 'INPUT_FILENAME' in line:\n",
    "            line = line.replace('input',fname_input[i])\n",
    "            print(line)\n",
    "            \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
